jQuery.noConflict();

jQuery(document).ready(function()
{
	/* begin update item hidden value */ 
	function onSelectChange($get_id, $check_id, $cur_id){
		if(jQuery($check_id).attr('id') == undefined){
			$get_table = "#update_" + $cur_id;
			var htmlToAdd = "<input type=\"hidden\" name=\"update_id[]\" value=\""+$get_id+"\" id=\"up_slider"+$get_id+"\">";
			jQuery($get_table).append(htmlToAdd);
		} 
	}
	/* end update item hidden value */ 

	/* getting skin informations */
	$skin1and2width = jQuery(".skin1and2width").attr('id');
	$skin3and4width = jQuery(".skin3and4width").attr('id');
	$skinheight = jQuery(".skinheight").attr('id');
	
	/* get template url */
	$get_template_url = jQuery(".template_url").attr('id');

	/* get slider count id */
	$get_count = jQuery("#count").attr('name');
	var newRowNum = $get_count;

	// begin add new row
	jQuery("#addnew").click(function(){
		
		newRowNum++;

		// adding new html table row
		var htmlToAdd = "<table class=\"slider_new_table\" id=\"" + newRowNum + "\">"
			
			+ "<tr class=\"slider_new_tr\">"
			
				+ "<td rowspan=\"2\" style=\"width:1%;\"><strong>&nbsp;</strong></td>"
				
				// preview image
				+ "<td style=\"width:3%\"><img src=\""+$get_template_url+"/images/admin/look.png\" alt=\"\" /></td>"
				
				// upload textarea
				+ "<td style=\"width:30%;\" class=\"types\"><textarea type=\"text\" name=\"slider" + newRowNum + "\" id=\"slider" + newRowNum + "\" class=\"upload_input\" style=\"width:95%;\" /></textarea><input type=\"hidden\" name=\"new_id[]\" value=" + newRowNum + ">"
				+ "<span class=\"tablespan\">Image</span> <input name=\"types" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"image\" checked=\"checked\"/> "
				+ "<span class=\"tablespan\">Video (with embed code)</span> <input name=\"types" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"video\"/>"
				+ "</td>"
				
				// upload button
				+ "<td style=\"width:10%;\"><div class=\"upload_tab\">"
				+ "<a href='media-upload.php?post_id="+newRowNum+"&amp;type=image&amp;TB_iframe=true' id=\"slider" + newRowNum + "\" class='set_input thickbox' title='Add an Image'><img src=\""+$get_template_url+"/images/admin/upload.png\" alt=\"\" />"
				+ "<br /><span style=\"float:left; display:table;\"><span class=\"tablespan\" id=\"skins" + newRowNum + "\">width: <strong>"+$skin1and2width+"px</strong><br />height: <strong>"+$skinheight+"px</strong></span></span></a>"
				+ "</div></td>"
				
				// title image & text
				+ "<td style=\"width:40%;\"><textarea class=\"up_skins"+newRowNum+"\" type=\"text\" name=\"title" + newRowNum + "\" style=\"width:95%;\"></textarea></td>"
				+ "<td style=\"width:5%;\"><img class=\"up_skins"+newRowNum+"\" src=\""+$get_template_url+"/images/admin/title.png\" alt=\"\" /></td>"
				+ "<td rowspan=\"2\" style=\"width:3%; text-align:center;\"><br /><br /><a href=\"\" class=\"remove\"><img src=\""+$get_template_url+"/images/admin/cancel.png\" alt=\"\" /></a></td>"

			+ "</tr>"
			
			+ "<tr class=\"slider_new_tr\">"
			
				// link image & text & read more button & image border & target
				+ "<td style=\"width:3%\"><img src=\""+$get_template_url+"/images/admin/link.png\" alt=\"\" /></td>"
				+ "<td style=\"width:30%;\"><input type=\"text\" name=\"url" + newRowNum + "\" style=\"width:95%;\"/>"
				+ "<br /><span class=\"tablespan\">Link with read more button</span> <input type=\"checkbox\" value=\"on\" name=\"readmore" + newRowNum + "\" />"
				+ "<br /><span class=\"tablespan\">Image border ( on/off ) </span> <input type=\"checkbox\"  value=\"on\" name=\"border" + newRowNum + "\" checked=\"checked\" />"
				+ "</td>"
				+ "<td style=\"width:10%;\"><select name=\"target"+newRowNum+"\" id=\"slider" + newRowNum + "\" style=\"width:70px;\"><option>_self</option><option>_blank</option></select></td>"
				
				// description text & image
				+ "<td style=\"width:40%;\"><textarea class=\"up_skins"+newRowNum+"\" name=\"description"+newRowNum+"\" id=\"slider" + newRowNum + "\" style=\"width:95%;\"></textarea></td>"
				+ "<td style=\"width:5%;\"><img class=\"up_skins"+newRowNum+"\" src=\""+$get_template_url+"/images/admin/content.png\" alt=\"\" /></td>"
				
			+ "</tr>"
				
			+ "<tr class=\"slider_new_tr\">"
				+ "<td colspan=\"4\"></td>"
				
				// slider skins
				+ "<td>"
				+ "<table class=\"slider_skin_table\" id=\"" + newRowNum + "\">"
				+ "<tr class=\"radios\">"
				+ "<td><img src=\""+$get_template_url+"/images/admin/model1.png\" alt=\"\" /><br/><input name=\"skins" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"skin1\" checked=\"checked\"/></td>"
				+ "<td><img src=\""+$get_template_url+"/images/admin/model2.png\" alt=\"\" /><br/><input name=\"skins" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"skin2\" /></td>"
				+ "<td><img src=\""+$get_template_url+"/images/admin/model3.png\" alt=\"\" /><br/><input name=\"skins" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"skin3\" /></td>"
				+ "<td><img src=\""+$get_template_url+"/images/admin/model4.png\" alt=\"\" /><br/><input name=\"skins" + newRowNum + "\" id=\"slider" + newRowNum + "\" type=\"radio\" value=\"skin4\" /></td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"

			+ "</tr>";
			// ending new html table row
			
			// table row target & effect
			jQuery("#rowHolder #here").append(htmlToAdd);
			jQuery("#"+newRowNum).hide().fadeIn("normal");
			
			// new table row uploader button
			jQuery("a.thickbox").unbind("click");
			jQuery("a.thickbox").click(function(){
				var t = this.title || this.name || null;
				var a = this.href || this.alt;
				var g = this.rel || false;
				window.get_id = this.id;
				tb_show(t,a,g);
				this.blur();
				return false;
			});
			
			// new table row remove button
			jQuery("a.remove").unbind("click");
			jQuery("a.remove").click(function(){
			
				jQuery(this).parent().parent().parent().fadeOut("fast", function() {
					jQuery(this).remove();
				});
				return false;
				
			});
			
			// skin buttons
			jQuery(".radios input:radio").click(function() {
				skinselector(jQuery(this).attr('value'), jQuery(this).attr('id'), jQuery(this).attr('name') );
			});

			// type buttons
			jQuery(".types input:radio").click(function() {
				$get_val = jQuery(this).attr('value');
				if($get_val == "video"){
					jQuery(".upload_tab").hide();
				}else{
					jQuery(".upload_tab").show();
				}
			});
	
			return false;

	});
	// end add new row

	
	
	jQuery(".radios input:radio").click(function() {
		skinselector(jQuery(this).attr('value'), jQuery(this).attr('id'), jQuery(this).attr('name') );
		
		$g = jQuery(this).attr('title');
		$c = "#up_" + jQuery(this).attr('id');
		$d = jQuery(this).attr('id');
		onSelectChange($g, $c, $d);
		
	});
	
	
	function skinselector($get_radio_id, $get_span_id, $get_span_name ){

		$get_span_names = "#"+$get_span_name;
		$get_desc_name = ".up_"+$get_span_name;
		
		switch($get_radio_id){
			case "skin1":
			case "skin2":
				$skinw = $skin1and2width+"px";
				$skinh = $skinheight;
			break;						
			case "skin4":
				$skinw = $skin3and4width+"px";
				$skinh = $skinheight;
			break;
			case "skin3":
				$skinw = $skin3and4width+"px";
				$skinh = $skinheight-100;
			break;
		}
		
		if($get_radio_id == "skin4"){
			jQuery($get_desc_name).fadeTo("normal", 0.33);
			jQuery($get_desc_name).attr("disabled", true);
		}else{
			jQuery($get_desc_name).fadeTo("normal", 1);
			jQuery($get_desc_name).attr("disabled", false);	
		}
		
		jQuery($get_span_names).html("width: <strong>"+$skinw+"</strong><br />height: <strong>"+$skinh+"px</strong>").hide().fadeIn("normal");
	}
	
	jQuery('.skin4').each(function(){
		skinselector(jQuery(this).attr('value'), jQuery(this).attr('id'), jQuery(this).attr('name') );
	});
	

	jQuery('.fade_slider_auto_play').each(function()
	{
		jQuery(this).bind('change', function()
		{	
			$get_val = jQuery(this).val();
			
			if($get_val == "on"){
				jQuery('#fade_slider_speed').show();
			}else{
			    jQuery('#fade_slider_speed').hide();
			}
			
		});
	});
	
	
	jQuery("#picker1").keyup(function () {
		var value = jQuery(this).val();
    	jQuery("#skin_border, #skin_border_2").css("background-color", value)
    });
	
	jQuery("#picker1").bind('change', function()
	{	
		var value = jQuery(this).val();
		jQuery("#skin_border, #skin_border_2").css("background-color", value)
	});
	
});